package com.cn.algorithm02.class18;

import java.util.ArrayList;
import java.util.List;

/***
 * @author: hels
 * @description: 打印字符串所有的排序
 **/
public class C03_PrintAllPermutations {
    public static void permutations1(String s) {
        if (s.isEmpty()) {
            return;
        }

        char[] chars = s.toCharArray();
        List<Character> rest = new ArrayList<>();
        for (char ch : chars) {
            rest.add(ch);
        }
        System.out.println(rest);
        List<String> ans = new ArrayList<>();
        p(rest, "", ans);
        System.out.println(ans);
    }
    public static void p(List<Character> rest, String path, List<String> ans) {
        if (rest.isEmpty()) {
            ans.add(path);
        } else {
            int size = rest.size();
            for (int i = 0; i < size; i++) {
                Character cur = rest.get(i);
                // path = path + cur;
                rest.remove(i);
                p(rest, path + cur , ans);
                rest.add(i, cur);
            }
        }
    }

    public static void main(String[] args) {
        permutations1("abcde");
    }
}
